﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var se = Session["UserName"];
            if (se == null || string.IsNullOrEmpty(se.ToString()))
            {
                Response.Redirect("Login.aspx");
            }

            if (!IsPostBack) {
                FillData();
            }
        }



        private void FillData()
        {
            var sql = string.Format("select * from Task");
            var dt = DbHelper.GetData(sql);

            Gv1.DataSource = dt;
            Gv1.DataBind();
        }

        private string GetVal(int rowIndex, int colIndex)
        {
            var control = Gv1.Rows[rowIndex].Cells[colIndex];
            var res = control.Controls.Count > 0 ? ((TextBox)control.Controls[0]).Text : control.Text;
            return res;
        }




        //取消添加
        protected void Gv1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            Gv1.EditIndex = -1;
            FillData();
        }
        //添加
        protected void Gv1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            Gv1.EditIndex = e.NewEditIndex;
            FillData();
        }
        //更新
        protected void Gv1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetVal(e.RowIndex,0);
            var isOk = int.TryParse(idString,out int id);
            var pname = GetVal(e.RowIndex,1);
            var desc = GetVal(e.RowIndex,2);
            var Remark = GetVal(e.RowIndex,3);

            if (isOk)
            {
                //有id修改
                var sql = string.Format("update task set TaskName='{0}',TaskContent='{1}',TaskStatus='{2}' where Id='{3}'", pname, desc, Remark, id);
                DbHelper.Exe(sql);
            }
            else 
            {
                //没有id 添加
                var sql = string.Format("insert into Task(TaskName,TaskContent,TaskStatus)values('{0}','{1}','{2}')",pname,desc,Remark);
                DbHelper.Exe(sql);
            }
            Gv1.EditIndex = -1;
            FillData();
        }
        //删除
        protected void Gv1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetVal(e.RowIndex,0);
            var sql = string.Format("delete from Task where id={0}",id);
            DbHelper.Exe(sql);
            Gv1.EditIndex = -1;
            FillData();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //var tempDt = DbHelper.GetData("select * from Task");
            //var dt = tempDt.Copy();
            //dt.Rows.Add(dt.NewRow());
            //Gv1.DataSource = dt;
            //Gv1.EditIndex = dt.Rows.Count - 1;
            //Gv1.DataBind();

            Response.Redirect("TaskEditNew.aspx");
        }
        //分页
        protected void Gv1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            Gv1.PageIndex = e.NewPageIndex;
            FillData();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            var text = TextBox1.Text;
            var ss = string.Format("select * from Task where TaskName like '%{0}%' or TaskContent like '%{1}%'or TaskStatus like '%{2}%'or Remarks like '%{3}%'",text,text,text,text);
            var sql = DbHelper.GetData(ss);
            Gv1.DataSource = sql;
            Gv1.DataBind();

        }
    }
}